<?php

namespace apps\marriage\app\op\controller;

use apps\marriage\core\model\MemberProfileExchangeApply;
use apps\marriage\core\service\MemberProfileExchangeApplyService;
use apps\marriage\core\service\MemberProfileService;
use apps\marriage\core\service\UserService;
use think\Exception;
use think\Request;

class MemberProfileExchangeApplyController extends BaseMarriageOpController
{
    /**
     * @var MemberProfileExchangeApplyService
     */
    private $memberProfileExchangeApplyService;

    /**
     * @var UserService
     */
    private $userService;

    /**
     * @var MemberProfileService
     */
    private $memberProfileService;

    protected function init()
    {
        parent::init();
        $this->memberProfileExchangeApplyService = service('MemberProfileExchangeApply', SERVICE_NAMESPACE);
        $this->userService = service('User', SERVICE_NAMESPACE);
        $this->memberProfileService = service('MemberProfile', SERVICE_NAMESPACE);
    }

    public function lists(Request $request)
    {
        $params = [];
        $res = $this->memberProfileExchangeApplyService->pageListByParams($params, 10);
        foreach ($res['data']as &$rs) {
            $rs = $this->memberProfileExchangeApplyService->format($rs);
            $rs['apply_user'] = $this->userService->getByPk($rs['apply_uid']);
            $rs['apply_user'] = arrayOnly($rs['apply_user'], ['id', 'nick', 'avatar_url']);
            $rs['apply_member'] = $this->memberProfileService->getByUserId($rs['apply_uid']);

            $rs['receiver_user'] = $this->userService->getByPk($rs['member_uid']);
            $rs['receiver_user'] = arrayOnly($rs['receiver_user'], ['id', 'nick', 'avatar_url']);
            $rs['receiver_member'] = $this->memberProfileService->getByUserId($rs['member_uid']);
        }

        return $this->success($res);
    }

    public function audit(Request $request)
    {
        $id = $request->param('id', 0, 'intval');
        $status = $request->param('status');
        $remark = $request->param('remark', '');
        if($status == 'pass') {
            $status = MemberProfileExchangeApply::STATUS_PASS;
        } else if($status == 'reject') {
            $status = MemberProfileExchangeApply::STATUS_REJECT;
        } else {
            throw new Exception('参数错误');
        }
        $this->memberProfileExchangeApplyService->onAudit($id, $status, $remark);
        $apply = $this->memberProfileExchangeApplyService->getByPk($id);
        $apply = $this->memberProfileExchangeApplyService->format($apply);
        $apply['apply_user'] = $this->userService->getByPk($apply['apply_uid']);
        $apply['apply_user'] = arrayOnly($apply['apply_user'], ['id', 'nick', 'avatar_url']);
        $apply['apply_member'] = $this->memberProfileService->getByUserId($apply['apply_uid']);

        $apply['receiver_user'] = $this->userService->getByPk($apply['member_uid']);
        $apply['receiver_user'] = arrayOnly($apply['receiver_user'], ['id', 'nick', 'avatar_url']);
        $apply['receiver_member'] = $this->memberProfileService->getByUserId($apply['member_uid']);

        return $this->success(['apply' => $apply]);
    }
}